package com.coocaa.coolink_tvos_mobile_demo.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.coocaa.coolink_tvos_mobile_demo.databinding.FragmentDashboardBinding;
import com.coocaa.coolink_tvos_mobile_demo.model.Key;
import com.coocaa.obrr_sdk.easyinterface.IObrrSDKEasy;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;
    private SendKeyBoardListener listener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener = (SendKeyBoardListener) getActivity();
    }

    public void sendKey(Key key) {
        String id = listener.sendKeyBoard(new IObrrSDKEasy.ISendContentResult() {
            @Override
            public void onResult(boolean isFinished, String msgID, String msg) {
                if (isFinished) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(),"发送成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                }else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(),"发送失败:"+msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        },key);
        if( id == null ){
            Toast.makeText(getActivity(),"请先连接上设备！", Toast.LENGTH_LONG).show();
        }
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        binding.upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理up_button的点击事件
                // public String sendContent(ISendContentResult iSendResult, String remoteSID, String contentName, String content,Boolean canLost)
                sendKey(Key.Up);
            }
        });

        binding.downButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理down_button的点击事件
                sendKey(Key.Down);
            }
        });

        binding.leftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理left_button的点击事件
                sendKey(Key.Left);
            }
        });

        binding.rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理right_button的点击事件
                sendKey(Key.Right);
            }
        });

        binding.okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理ok_button的点击事件
                sendKey(Key.Enter);
            }
        });

        binding.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理back_button的点击事件
                sendKey(Key.Esc);
            }
        });

        binding.menuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理menu_button的点击事件
                // 用F1？
                sendKey(Key.F1);
            }
        });

        binding.sourceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理source_button的点击事件
                // 用F2？
                sendKey(Key.F2);
            }
        });

        binding.homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理home_button的点击事件
                sendKey(Key.Home);
            }
        });

        binding.powerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理power_button的点击事件
                // 用F3？
                sendKey(Key.F3);
            }
        });
//        final TextView textView = binding.textDashboard;
//        dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public interface SendKeyBoardListener {
        // return messageID, same with ISendKeyBoardResult messageID.
        String sendKeyBoard(IObrrSDKEasy.ISendContentResult resultListener, Key key);
    }
}